package com.personal.common.util;


import java.util.List;
import java.util.Random;

public class RandomUtil {
	/**
	 * 产生大于等于min 小于max 的整数
	 * @param max
	 * @param min
	 * @return
	 */
	public static int random(int min,int max){
		Random random = new Random();
		return random.nextInt(max - min) + min;
	}
	
	public static int random(int max){
		Random random = new Random();
		return random.nextInt(max);
	}
	
	/**
	 * 根据随机数，获取概率数组下标
	 * 
	 * @param changes
	 *            概率数组
	 * @param randomValue
	 *            根据 概率数组加起来的和 +1 生成的随机数
	 * @return
	 */
	public static int getIndex(int[] changes, int randomValue) {
		int i = 0;
		int temp = 0;
		for (; i < changes.length; i++) {
			temp += changes[i];
			if (randomValue <= temp) {
				break;
			}
		}
		return i;
	}
	
	public static void main(String[] args) {
		for (int i = 0; i < 100; i++) {
			System.out.println(random(3,10));
		}
	}

	public static int getIndex(Integer[] changes, int randomValue) {
		int i = 0;
		int temp = 0;
		for (; i < changes.length; i++) {
			temp += changes[i];
			if (randomValue <= temp) {
				break;
			}
		}
		return i;
	}

	public static int getIndex(List<Integer> changes, int randomValue) {
		int i = 0;
		int temp = 0;
		for (; i < changes.size(); i++) {
			temp += changes.get(i);
			if (randomValue <= temp) {
				break;
			}
		}
		return i;
	}
}
